package cc.devclub.ftd.thread.lifecycle;

/**
 * Created by loading on 2021/12/13.
 */
public class MyThreadState3 implements Runnable {


    public static void main(String[] args) throws InterruptedException {

        Thread thread1 = new Thread(new MyThreadState3());

        thread1.start();

        Thread.State state1 = thread1.getState();


        // 只要线程未运行结束，就一直输出线程当前的状态
        while (state1 != Thread.State.TERMINATED) {
            Thread.State mainState = Thread.currentThread().getState();
            state1 = thread1.getState();
            // 观察当前线程状态
            System.out.println("线程1状态为:" + state1);
            System.out.println("主线程状态为:" + mainState);
            thread1.join();
        }
    }

    @Override
    public void run() {
        int i = 100;
        while (i > 0) {
            i--;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


